package com.cz.fra.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.cz.fra.entity.Comment;
import com.cz.fra.service.CommentService;
import com.cz.fra.service.ServiceException;
import com.cz.fra.service.impl.CommentServiceImpl;

public class RetrieveCommentsServlet extends HttpServlet{

	 /**
	 * default serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	
	CommentService commentService = new CommentServiceImpl();
	
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
		int reportId = Integer.parseInt(request.getParameter("reportId"));
		List<Comment> commentList = null;
	    try {
	    	commentList = commentService.getCommentListByReportID(reportId);
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    PrintWriter out = response.getWriter();
	     JSONArray array = new JSONArray();
	     for(Comment c : commentList){
	    	 JSONObject obj = new JSONObject();
	    	 try{
	    		 obj.put("cid", c.getCommentId());
	    		 obj.put("reportId", c.getReportId());
	    		 obj.put("content", c.getContent());
	    		 obj.put("uid", c.getCommentBy());
	    		 obj.put("commentDate", c.getCommentDate());
	    	 }catch(Exception e){
	    		 e.printStackTrace();
	    	 }
	    	 array.put(obj);
	     }
	     out.write(array.toString());
	     out.flush();
	     out.close();
	}
	
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
		doGet(request,response);
	}
	
}
